local biz_name = "_adv.gif"

ConfigRedisConnectors[biz_name]={
    host = "127.0.0.1",
    port = 6379,
    unixpath = "/etc/redis/unixpath/redis.sock",
    db = NIL,
    user = NIL,
    passwd = NIL,
    queue = "queue_1"
}

local function _adv_gif()
  return coroutine.create(function ()
    local REDIS_SEPERATOR = config.SEPARATE_SYN
    local local_config = config
    local local_reply = reply
    local local_request = request
    local local_luredis = luredis
    local os_time = os.time
    local local_type = type
    local paraTable, headerTable, cookieString, cookieTable
    local create_time, ip, user_agent, newguid, guid, create_cookie
    local aditem, adm_domain, zoneID, campaign, chanelID, refererHeader
    local cmd_format = "lpush "..ConfigRedisConnectors[biz_name].queue.." %s "
    while true do
        local_reply.pushHeader(local_config.CACHE_CONTROL_HEADER, "private, no-cache, no-cache=Set-Cookie, proxy-revalidate")
        local_reply.pushHeader(local_config.PRAGMA_HEADER, "no-cache")
        local_reply.pushHeader(local_config.EXPIRES_HEADER, "Wed, 17 Sep 1975 21:32:10 GMT")
        local_reply.pushHeader(local_config.CONTENT_TYPE_HEADER, "image/gif")
        local_reply.pushHeader(local_config.LAST_MODIFIED_HEADER, "Wed, 19 Apr 2000 11:43:00 GMT")
        local_reply.pushHeader("P3P", "CP=\"IDC DSP COR CURa ADMa OUR IND PHY ONL COM STA\"")
        local_reply.pushHeader("Content-Length", "0")
        local_reply.setStatus(local_reply.status_type.ok)
        local_luredis.clear_cmd_buffer()
        paraTable = local_request.getParameters_key_value();
        headerTable = local_request.getHeaders_key_value();
        cookieString = headerTable[local_config.COOKIE_HEADER];
        cookieTable = parseCookies(cookieString);
        create_time = os_time();
        local_luredis.append_cmd_buffer(create_time, REDIS_SEPERATOR)
        ip = local_request.getIP()
        local_luredis.append_cmd_buffer(ip, REDIS_SEPERATOR)
        user_agent = headerTable[local_config.USER_AGENT_HEADER];
        if user_agent == nil then user_agent = "" end
        -- nil value causes append buffer to crash
        local_luredis.append_cmd_buffer(user_agent, REDIS_SEPERATOR)
        newguid = 0;
        guid = cookieTable[local_config.GUID_COOKIE];
        create_cookie = cookieTable[local_config.CREATE_COOKIE]
        if guid then
           local_luredis.append_cmd_buffer(create_cookie)
        else
           guid = create_guid(create_time, ip);
           local_luredis.append_cmd_buffer(create_time)
           if user_agent then
               if not(string.find(user_agent, "Safari")
                  and (not string.find(user_agent, "Chrome")))
                 then
                     --Them guid cookie
                     pushCookieHeader(local_config.GUID_COOKIE, guid, local_config.EXPIRE_TIME+create_time, "/", local_config.http.setcookie.domain, false)
                     --Them create cookie
                     pushCookieHeader(local_config.CREATE_COOKIE, create_time, local_config.EXPIRE_TIME+create_time, "/", local_config.http.setcookie.domain, false)
                 end
             end
             newguid = 1;
        end
        local_luredis.append_cmd_buffer(REDIS_SEPERATOR)
        aditem = paraTable[local_config.ADM_ADITEM_PARAM];
        if not((tonumber(aditem) ~= nil)and (aditem ~= nil)) then
             aditem = local_config.ADM_ADITEM_STRING_DEFAULT
        end
        local_luredis.append_cmd_buffer(aditem, REDIS_SEPERATOR)
        local_luredis.append_cmd_buffer(1, REDIS_SEPERATOR)
        local_luredis.append_cmd_buffer(guid, REDIS_SEPERATOR)

        adm_domain = paraTable[local_config.ADM_DOMAIN_PARAM]
        if not adm_domain then adm_domain = "" end
        local_luredis.append_cmd_buffer(adm_domain, REDIS_SEPERATOR)
        local_luredis.append_cmd_buffer(0, REDIS_SEPERATOR)
        local_luredis.append_cmd_buffer(0, REDIS_SEPERATOR)

        zoneID = paraTable[local_config.ADM_ZONEID_PARAM];
        if not(local_type(zoneID) == "number") then
               zoneID = local_config.ADM_ZONEID_STRING_DEFAULT
        end
        local_luredis.append_cmd_buffer(zoneID, REDIS_SEPERATOR)

        campaign = paraTable[local_config.ADM_CAMPAIGN_PARAM];
        if not(local_type(campaign) == "number")  then
               campaign = local_config.ADM_CAMPAIGN_STRING_DEFAULT
        end
        local_luredis.append_cmd_buffer(campaign, REDIS_SEPERATOR)

        chanelID = paraTable[local_config.ADM_CHANNELID_PARAM];
        if not(local_type(chanelID) == "number")  then
               chanelID = local_config.ADM_CHANNELID_STRING_DEFAULT
        end

        local_luredis.append_cmd_buffer(chanelID, REDIS_SEPERATOR)
        local_luredis.append_cmd_buffer(newguid, REDIS_SEPERATOR)
        refererHeader = headerTable[local_config.REFERER_HEADER];

        local_luredis.append_cmd_buffer(refererHeader, REDIS_SEPERATOR)
        util.redis_command(biz_name,false,local_luredis.send_cmd_buffer,cmd_format)

        coroutine.yield()
        end
      end)
    end
f[biz_name] = _adv_gif()
